Sub Main ()
with metascript 
.exec "update salehead set getirn=0 where getirn is null", .getdatafile
.exec "update salehead set gotirn=0 where gotirn is null", .getdatafile
.exec "update salehead set irnNo='' where irnNo is null", .getdatafile
'.exec "update s set s.gotirn=1, s.irnno=i.IRN from SaleHead as s, IRN_INFO as i where s.BillNo = i.DOCUMENT_NUMBER and s.GetIRN=1 and s.GotIRN=0", .getdatafile

.showform "Select 0 as s, Code, Name, BillNo, BillDate , Payrupees, salekey from salehead , customer where customerkey = customerid and saletype in (select serieskey from series where hideseries=0) and billdate between '" & local.dtfrom.tag & "' and '" & local.dtto.tag & "'" & _
  " and GetIRN=0 and GOTIRN=0 and len(GSTIN)=15 and len(pin)=6", .getdatafile, s1, s2

if trim(s1)<>"" then 

st = "insert into girn_inp(BuyerAddr1, BuyerAddr2, BuyerEm, BuyerGSTIN, BuyerLglNm, BuyerLoc, BuyerPh, BuyerPin, BuyerPOS, BuyerStCd, BuyerTrdNm, DocDt, DocNo, DocTyp,  itmUnit, ItmAssAmt, itmCgstAmt, ItmDisc, ItmFreeQty, ItmGstRt, itmHsnCd, itmIgstAmt, ItmIsServc, itmOthChrg, itmPrdDesc, ItmQty, itmSgstAmt, ItmTotAmt, ItmTotVal, ItmUntPrc, SlrAddr1, SlrAddr2, SlrEm, SlrGSTIN, SlrLglNm, SlrLoc, SlrPh, slrPin, SlrStCd, SlrTrdNm, TraSupTyp, ValAssVal, ValCGSTVal, ValIGSTVal, ValOthChrg, valRdOfAmt, ValSGSTVal, ValTotVal, ValTotValF, EwbDist)"
st = st & " select BuyerAddr1, BuyerAddr2, BuyerEm, BuyerGSTIN, BuyerLglNm, BuyerLoc, BuyerPh, BuyerPin, BuyerPOS, BuyerStCd, BuyerTrdNm, convert(varchar(10),DocDt,103), DocNo, DocTyp, 'UNT',  ItmAssAmt, itmCgstAmt, ItmDisc, ItmFreeQty, ItmGstRt, itmHsnCd, itmIgstAmt, ItmIsServc, itmOthChrg, itmPrdDesc, ItmQty, itmSgstAmt, ItmTotAmt, ItmTotVal, ItmUntPrc, SlrAddr1, SlrAddr2, SlrEm, SlrGSTIN, SlrLglNm, SlrLoc, SlrPh, slrPin, SlrStCd, SlrTrdNm, TraSupTyp, ValAssVal, ValCGSTVal, ValIGSTVal, ValOthChrg, valRdOfAmt, ValSGSTVal, ValTotVal, ValTotValF, case when slrpin=buyerpin then 25 else 0 end " & _
" from mygsthead as h, mygstitems as i where h.salekey = i.salekey and h.salekey in (" & s1 & ")" 

if msgbox("Mark selected Invoices for IRN Submission?", vbquestion + vbyesno,"Confirm") = vbyes then
  .exec "Update Salehead set GetIRN=1 where salekey in ("   & s1 & ")", .getdatafile
  .exec st & "", .getdatafile

set rs = .sumrs("Select count(*) as salebills from salehead where salekey in (" & s1 & ") ", .getdatafile)
bills = rs.fields("salebills")
set rs = .sumrs(" select distinct docno from girn_inp where docno in ( Select billno from salehead where salekey in (" & s1 & ")) ", .getdatafile)
added = rs.recordcount

   msgbox "Bills Selected " & bills & " Added " & added & vbcrlf & " Done",vbinformation,"PharmaPulse"
else
  msgbox "Operation cancelled by user"
end if
end if

if msgbox("Do you want to see Customers who need PinCode updation?", vbquestion + vbyesno+ vbdefaultbutton2) = vbyes then
set rs = .sumrs("Select Code, Name, gstin, idarea, areaname, cell, phone, PIN, customerkey from customer where customerkey  in (select customerid from salehead ) " & _
  " and len(GSTIN)=15 and len(pin)<>6  ", .getdatafile)
filename = local.getfilename("Enter filename to save customer list", "xls")
local.dumprstoexcel rs, filename

.showform "Select 0 as s, Code, Name, count(*) as Invoices, sum(Payrupees) as amount, PIN, customerkey from salehead , customer where customerkey = customerid and saletype in (select serieskey from series where hideseries=0) and billdate between '" & local.dtfrom.tag & "' and '" & local.dtto.tag & "'" & _
  " and len(GSTIN)=15 and len(pin)<>6 Group by code,name,pin,customerkey ", .getdatafile, s1, s2

end if

end with 
End Sub




